<template>
<u--form
	class="page1"
	labelPosition="left"
	labelWidth="80"
	:model="form"
	:rules="rules"
	ref="uForm"
>
	<u-form-item
		label="购买物品"
		prop="name"
		borderBottom
	>
		<u--input v-model="form.name" placeholder="购买物品的名称数量等信息"></u--input>
	</u-form-item>
	<u-form-item
		label="购买价格"
		prop="buyPrice"
		borderBottom
	>
		<u--input v-model="form.buyPrice" type="digit" placeholder="进货时汉服的购买价格"></u--input>
	</u-form-item>
	<u-form-item
		label="备注"
		prop="remark"
		borderBottom
	>
		<u--textarea v-model="form.remark" placeholder="请输入备注内容"></u--textarea>
	</u-form-item>
	<u-button @click="submit" type="primary">提交</u-button>
</u--form>
</template>

<script setup>
import { onLoad, onUnload } from '@dcloudio/uni-app'
import { useBillStore } from '@/stores'
import { dynasty } from '@/utils/data'

const bill = useBillStore()
const uForm = ref() 
const form = reactive({
	name: '',
	buyPrice: '', // 进货价格
	remark: '',
})
const rules = reactive({
	name: [
		{
			required: true,
			message: '请输入物品的名称',
			trigger: ['blur', 'change']
		}
	],
	buyPrice: [
		{
			required: true,
			message: '请输入物品的价格',
			trigger: ['blur', 'change']
		}
	],
})
let currentMonth='';

function submit() {
	uForm.value.validate().then(res => {
		let msg;
		if(form.timestamp){
			msg = '编辑'
			// 更新商品数据
			bill.editItem(form)
		}else{
			msg = '添加'
			bill.addItem({...form, timestamp: Date.now()}, currentMonth)
		}
		// const a = {message: `${msg}成功`, type: 'success', complete(){}}
		// uni.$u.toast(`${msg}成功`)
		uni.showToast({icon: 'success', title: `${msg}成功`, duration: 2000})
		setTimeout(uni.navigateBack, 1000)
	}).catch(errors => {
		// uni.$u.toast('校验失败')
	})
}
onLoad((option)=>{
	if(option&&option.currentMonth){
		currentMonth = option.currentMonth;
		uni.setNavigationBarTitle({title: `添加${currentMonth}报账`})
	}
	// 编辑的时候先获取数据 然后填充表单
	if(option&&option.order){
		uni.setNavigationBarTitle({title: "编辑报账信息"})
		const data = JSON.parse(option.order)
		for(let key in data){
			form[key] = data[key]
		}
	}
})
onUnload(()=>{

})
</script>

<style>
</style>